import 'package:family_expense/features/account_user/models/user_active_token.dart';
import 'package:my_flutter_core/my_flutter_core.dart';

class UserActiveTokenService {
  final String _url = '/user-active-token';
  final HttpRequestService _httpRequestService = HttpRequestService();

  /// Retrieves current user all [UserActiveToken] device info from backend service.
  Future<List<UserActiveToken>> queryDeviceByUsername() async {
    // Gets the response of the http get request.
    final response =
        await _httpRequestService.get('$_url/query-device-by-username');

    // Parses the data from [Map<String, dynamic>] to [Region].
    return (response.payload as List<dynamic>)
        .map((p) => UserActiveToken.fromJson(p))
        .toList();
  }

  /// Updates device [enabled] by [id].
  Future<void> updateEnabled(String id, bool enabled) async {
    _httpRequestService.post('$_url/update-enabled', body: {
      'id': id,
      'enabled': enabled,
    });
  }

  /// Deletes with [id].
  Future<void> delete(String id) async {
    // Deletes data from server.
    _httpRequestService.post('$_url/delete', body: {'id': id});
  }
}
